package com.jbp.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jbp.dao.WalletDao;
import com.jbp.exception.RRException;
import com.jbp.model.Wallet;
import com.jbp.service.WalletService;
import org.springframework.stereotype.Service;
import com.jbp.utils.PageUtils;
import com.jbp.utils.Query;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 用户钱包(Wallet)表服务实现类
 * @author cc
 * @since 2020-06-20 09:43:00
 */
@Service
public class WalletServiceImpl extends ServiceImpl<WalletDao, Wallet> implements WalletService {
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<Wallet> page = this.page(
                new Query<Wallet>().getPage(params),
                new QueryWrapper<>()
        );

        return new PageUtils(page);
    }

    @Override
    public Wallet getByUserId(Integer userId) {
        QueryWrapper<Wallet> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        return getOne(queryWrapper);
    }

    //数据更新，加乐观锁
    @Override
    public void updateWithLock(Wallet wallet) {
        wallet.setUpdateTime(new Date());
        Integer result = getBaseMapper().updateWithLock(wallet);
        if (null == result || result < 1) {
            throw new RRException("服务器忙");
        }
    }


}